//
// PCAgent resource manager.

#ifndef LONGKEY_PCAGENT_RESOURCE_MANAGER_H_
#define LONGKEY_PCAGENT_RESOURCE_MANAGER_H_

#include <Windows.h>
#include <atlstr.h>
#include <map>
#include <vector>
#include "../base/synchronized.h"

class ResourceManager
{
public:
	// Create must be called before going multithreaded.
	static HRESULT CreateForDefaultLanguage(bool is_machine, const CString& resource_dir);

	static HRESULT Create(bool is_machine, const CString& resource_dir, const CString& lang);

	static void Delete();

	static ResourceManager& Instance();

	static void GetSupportedLanguageDllNames(std::vector<CString>* filenames);

	// Get resource DLL handle for the given language. DLL will be loaded if necessary.
	HRESULT GetResourceDll(const CString& language, HINSTANCE* dll_handle);

private:
	struct ResourceDllInfo
	{
		ResourceDllInfo() : dll_handle(NULL) {}

		HMODULE dll_handle;
		CString file_path;
		CString language;
	};

	ResourceManager(bool is_machine, const CString& resource_dir);
	~ResourceManager();

	// The resource manager tries to load the resource dll corresponding to
	// the language in the following order:
	// 1. Language parameter.
	// 2. Language in the registry.
	// 3. First file returned by NTFS in the module directory.
	HRESULT LoadResourceDll(const CString& language, ResourceDllInfo* dll_info);
	HRESULT SetDefaultResourceByLanguage(const CString& language);
	HRESULT LoadLibraryAsDataFile(const CString& filename, ResourceDllInfo* dll_info) const;

	// Gets resource DLL info for the given language. DLL will be loaded if
	// necessary.
	HRESULT GetResourceDllInfo(const CString& language, ResourceDllInfo* dll_info);

	static CString GetResourceDllName(const CString& language);

	LLock lock_;
	typedef std::map<CString, ResourceDllInfo> LanguageToResourceMap;

	bool is_machine_;
	CString resource_dir_;
	LanguageToResourceMap resource_map_;
	HINSTANCE saved_atl_resource_;

	static ResourceManager* instance_;

	DISALLOW_EVIL_CONSTRUCTORS(ResourceManager);
};

#endif	// LONGKEY_PCAGENT_RESOURCE_MANAGER_H_